<?php
class Admin_Form_Route extends TBB_Form
{	
	public function init() {			
		// set attributes for main form
		$this->addAttribs(array(
			'class' => 'span-14',
			//'style' => 'background-color: #ccc;'
		));
		
		///////////////////// create id element  /////////////////////
		$id = $this->_createRouteIDElement();		

		///////////////////// create boarding point element /////////////////////
		$boardingPoint = $this->_createBoardingPointElement();										      	
		
		///////////////////// create dropping point element  /////////////////////
		$droppingPoint = $this->_createDroppingPointElement();	
		
		/////////////////// create submit element  /////////////////////
		$submit = $this->_createSubmitButton();
		
		// add all elements
		$this->addElements(array($id, $boardingPoint, $droppingPoint, $submit));
		$this->addDisplayGroup(array('routeID', 'boardingPoint', 'droppingPoint', 'submit'),
							  'routeGroup',
							  array('class' => 'fieldsetForm span-7')
		);
		
		// set decorators for form
		$this->setDecorators($this->_formDecorators);
		$this->setDisplayGroupDecorators($this->_displayGroupDecorators);
	}	
	
	private function _createRouteIDElement()
	{
		$id = $this->createElement ( 'hidden', 'routeID' );
		$id->setDecorators ( $this->_hiddenDecorators );
		$id->setAttribs(array(
			'class' => 'span-4',
		));
		return $id;
	}
	
	public function addFromToCityElement($cities = array())
	{
		// two these validators not working. wtf??? It seems to be that select form element can't be validated!!!
		$diffCitiesValidator = new TBB_Validate_DifferentCities();
		$uniqueRouteValidator = new TBB_Validate_UniqueRoute(new TBB_Model_Route());
		// create from element
		$from = $this->createElement('select', 'from')
					 ->setLabel('Từ: ')
			 		 ->setMultiOptions($cities)			 		 		
			 		 ->setOrder(1);
		$from->setAttribs(array(
			'class' => 'span-6'
		));
		$from->getDecorator('Label')->setOption('class', 'span-4 labelDecorator');
		$from->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');
				
		// create to element		
		$to = $this->createElement('select', 'to')
				   ->setLabel('Đến: ')
			 	   ->setMultiOptions($cities)
			 	   ->setValue('Bắc Giang')
			 	   ->setValidators(array(array($diffCitiesValidator, true),
			 	   						 array($uniqueRouteValidator, true)
			 	   ))
			 	   ->setOrder(2);
		$to->setAttribs(array(
			'class' => 'span-6'
		));		
		$to->getDecorator('Label')->setOption('class', 'span-4 labelDecorator');
		$to->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');		
		
		// add from and to elements to display group
		$this->getDisplayGroup('routeGroup')->addElements(array($from, $to));		
	}
	
	public function setFromToCityValue($fromValue, $toValue)
	{		
		if($this->getDisplayGroup('routeGroup')->getElement('from') &&
		   $this->getDisplayGroup('routeGroup')->getElement('to')) {
			$this->getDisplayGroup('routeGroup')->getElement('from')->setValue($fromValue);
			$this->getDisplayGroup('routeGroup')->getElement('to')->setValue($toValue);
		}
	}
	
	public function _createBoardingPointElement()
	{
		$boardingPointNotEmptyValidator = $this->_createNotEmptyValidator('Làm ơn nhập trạm đi');
		$boardingPoint = $this->createElement('text', 'boardingPoint')
						 	  ->setLabel ( 'Trạm đi: ' )
						      ->setRequired ( true )
						 	  ->addFilter ( 'StripTags' )
						 	  ->setValidators(array(array($boardingPointNotEmptyValidator, true)
						 	  ));
		$boardingPoint->setAttribs(array(
			'class' => 'span-6'
		));	
		$boardingPoint->getDecorator('Label')->setOption('class', 'span-4 labelDecorator');
		$boardingPoint->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');
		return $boardingPoint;
	}
	
	public function _createDroppingPointElement()
	{
		$diffValidator = new TBB_Validate_DifferentPoint();
		$droppingPointNotEmptyValidator = $this->_createNotEmptyValidator('Làm ơn nhập trạm đến');
		$droppingPoint = $this->createElement('text', 'droppingPoint')
						 	  ->setLabel ( 'Trạm đến: ' )
						      ->setRequired ( true )
						 	  ->addFilter ( 'StripTags' )
						 	  ->setValidators(array(array($droppingPointNotEmptyValidator, true),
						 	  						array($diffValidator, true)
						 	  ));
		$droppingPoint->setAttribs(array(
			'class' => 'span-6'
		));	
		$droppingPoint->getDecorator('Label')->setOption('class', 'span-4 labelDecorator');
		$droppingPoint->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');
		return $droppingPoint;
	}
	
	private function _createSubmitButton()
	{
		$submit = $this->createElement ( 'submit', 'submit' );
		$submit->setLabel ( 'Chấp nhận' );
		$submit->setDecorators($this->_submitDecorators);
		$submit->getDecorator('HtmlTag')->setOption('id', 'createRouteButton');
		$submit->setAttribs(array(
			'class' => 'span-4 submitDecorator',
		));
		return $submit;
	}
	
	
}